//
//  RootViewController.m
//  UI_豆瓣影院
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"
#import "MyTableViewCell.h"
#import "ActiveModel.h"
#import "UIImageView+WebCache.h"
#import "Root1ViewController.h"
#import "AAALHXDateHandle.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableview;


@end

@implementation RootViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"] forBarMetrics:UIBarMetricsDefault];
    }
    return self;
    
    
}
- (void)dealloc {
    [_tableview release];
    [_array release];
    [super dealloc];
    
    
}


- (void)viewDidLoad {
   
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    [_tableview registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableview.delegate =self;
    _tableview.dataSource = self;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"活动";
    [_tableview release];
    self.array = [NSMutableArray array];
    
    
    [self dataHunde1];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.zLable.image = [UIImage imageNamed:@"bg_eventlistcell@2x.png"];
    ActiveModel *model = _array[indexPath.row];
    cell.lable.font = [UIFont boldSystemFontOfSize:20];
    cell.lable.text = model.title;
    cell.gLable.textColor = [UIColor redColor];
    cell.gLable.text = [model.participant_count stringValue];
    cell.gLable1.text = @"感兴趣: ";
    cell.cLable.textColor = [UIColor redColor];
    cell.cLable.text = [model.wisher_count stringValue];
    cell.cLable1.text =@"参加: ";
   
    
    NSString *str1 = model.begin_time;
    NSString *str2 = [str1 substringWithRange:NSMakeRange(5, 11)];
    NSString *str3 = model.end_time;
    NSString *str4 = [str3 substringWithRange:NSMakeRange(5, 11)];
    NSString *str5 = [NSString stringWithFormat:@"%@ -- %@", str2, str4];
   
    
    cell.tLable.font = [UIFont systemFontOfSize:14];
    cell.tLable.text = str5;
    cell.dLable.font = [UIFont systemFontOfSize:15];
    cell.dLable.text = [NSString stringWithFormat:@"%@", model.address];
   
    
    cell.view2.image = [UIImage imageNamed:@"icon_date@2x.png"];
    cell.view3.image = [UIImage imageNamed:@"icon_spot@2x.png"];
    cell.view4.image = [UIImage imageNamed:@"icon_catalog@2x.png"];
    cell.lLable.text = [NSString stringWithFormat:@"%@%@", @"类型: ", model.category_name];
    
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.image]];
    
    
    return cell;
    
}

// 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 285;
    
}

// cell 的 个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _array.count;
}

// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Root1ViewController *vc = [[Root1ViewController alloc] init];
    vc.model = _array[indexPath.row];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    
    ActiveModel *model1 = _array[indexPath.row];
    
    CGRect rect = [model1.content boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    vc.heightl = rect.size.height;
    
    [self.navigationController pushViewController:vc animated:YES];
 }


// 数据处理
- (void)dataHunde1 {
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
   
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
   
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    if (error) {
       
    }
    self.arr = [NSMutableArray array];
   
    NSMutableArray * arr = result[@"events"];
    for (NSMutableDictionary *dic in arr) {
        ActiveModel *model = [ActiveModel actionModel];
        [model setValuesForKeysWithDictionary:dic];
        [_array addObject:model];
        [_arr addObject:model];
     
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
